package com.flot.yazb.base.adapter.recycleview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class RVAdapter<T> extends RecyclerView.Adapter<RvHolder> {

    private List<T> list = new ArrayList<>();//数据源
    private Context mContext;
    private OnItemClickListener mOnItemClickListener;
    protected ItemViewDelegateManager mItemViewDelegateManager;

    public void setList(List<T> list){
        this.list=list;
    }

    public void addReFreshData(List<T> nlist) {
        this.list=nlist;
        notifyDataSetChanged();
    }

    public void addRLoadMOreData(List<T> nlist) {
        if(nlist==null||nlist.isEmpty()){
            return;
        }
        this.list.addAll(nlist);
        notifyDataSetChanged();
    }

    public RVAdapter(Context context, List<T> datas) {
        this.list = datas;
        this.mContext = context;
        mItemViewDelegateManager = new ItemViewDelegateManager();
    }

    @Override
    public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType);
        int layoutId = itemViewDelegate.getItemViewLayoutId();
        RvHolder holder = RvHolder.createViewHolder(mContext, parent, layoutId);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RvHolder holder, final int position) {
        convert(holder, list.get(position), position);
        setOnListtener(holder);
    }

    private void convert(RvHolder holder, T t, int position) {
        mItemViewDelegateManager.convert(holder, t, position);
    }


    @Override
    public int getItemCount() {
        return list!=null?list.size():0;
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    protected void setOnListtener(final RecyclerView.ViewHolder holder) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, layoutPosition);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, layoutPosition);
                    return true;
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    interface ItemViewDelegate<T> {
        int getItemViewLayoutId();
        boolean isForViewType(T item, int position);
        void converts(RvHolder holder, T t, int position);
    };


    public RVAdapter<T> addItemViewDelegate(ItemViewDelegate<T> itemViewDelegate) {
        mItemViewDelegateManager.addDelegate(itemViewDelegate);
        return this;
    }

    /**
     * 移除但哥item
     * @param position
     */
    @Deprecated
    public void removeItem(int position){

    }

    /**
     * 添加但哥item
     * @param position
     */
    @Deprecated
    public void addItem(int position,T data){
        list.add(position, data);
        notifyItemInserted(position);
    }

}
